package Model;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.filechooser.*;
import Model.Trip;
import AirLine.Flight;
import AirLine.Seat;
import javax.swing.*;
import java.sql.*;


public class ResultPanel extends JPanel implements ActionListener{


	private JButton book;
        private ArrayList<JButton> buttonList;
        private ArrayList<JTable> tableList;
        private ArrayList<JPanel> panelList;
        private JPanel panel;        
        private TreeSet<Trip> trips;
        private HashMap<String, Trip> buttonMap;
        private Trip chosenTrip;
        
	public ResultPanel(TreeSet<Trip> trips)
	{
            //this.setPreferredSize(new Dimension (400, 400));
            this.trips=trips;
            panel=new JPanel();
            //panel=this;
            panelList=new ArrayList<JPanel>();
            buttonList=new ArrayList<JButton> ();
            tableList=new ArrayList<JTable>();
            buttonMap = new HashMap<String, Trip>();
            createTables();
            showTables();
       
            createWindow();
        }
        
        private void createWindow()
        {
            JFrame window = new JFrame("Jaeee");
            window.setPreferredSize(new Dimension(400, 700));
            //window.setResizable(false);
            //window.setLayout(new BorderLayout());
            //window.setBounds(50, 50, 300, 300);
            //window.setBackground(Color.BLUE);

            //window.setLayout(new BorderLayout());
            window.setContentPane(this);
            window.pack();
            
            window.setVisible(true);        // Display the window
            //panel.repaint();
           // window.repaint();  
        }
	
	public void createTables()
	{
            LinkedList<Flight> flights;
            String[] columns={"Airline", "Departure Airport", "Destination Airport", "Departure Time", "Destination Time", "Free Seats", "Price"};
            int j=0;
		for(Trip t: trips)
                {
                    
                   // DefaultTableModel model=new DefaultTableModel(7, t.getFlights().size()+1);       
                    JTable table=new JTable(t.getFlights().size()+1, 7);
                    
                    
                    for(int k=0; k<columns.length; k++)
                    {
                        table.setValueAt(columns[k], 0, k);
                    }
                    table.getTableHeader().setBackground(Color.yellow);
                    
                    table.repaint();
                     table.getTableHeader().repaint();
                     table.getTableHeader().setBackground(Color.blue);
                    
                    JPanel panel=new JPanel();
                    panel.setLayout(new GridBagLayout());
                    GridBagConstraints c = new GridBagConstraints();
                    c.fill = GridBagConstraints.HORIZONTAL;
                    c.gridx=0;
                    c.gridy =0;
                    int i=0;
                    for(Flight f: t.getFlights())
                    {
                        i++;
                        table.setValueAt(f.getAirline().getName(), i, 0);
                        table.setValueAt(f.getDepartureAirport(), i, 1);
                        table.setValueAt(f.getDestinationAirport(), i, 2);
                        table.setValueAt(f.getDepartureTime(), i, 3);
                        table.setValueAt(f.getArrivalTime(), i, 4);
                        table.setValueAt(f.getFreeSeats().size(), i, 5);
                        table.setValueAt(f.getPrice(), i, 6);
                        
                    }
                  // tableList.add(table);
                   JButton button=new JButton("Book");
                   String buttonID="tripID: "+j;
                   button.setActionCommand(buttonID);
                   button.addActionListener(this);
                   buttonMap.put(buttonID, t);
                   JTextField field=new JTextField(t.getDepartureAirport()+"->"+t.getDestinationAirport()+"  Duration: "+new Time(System.currentTimeMillis()+t.getDuration()));
                   
                   panel.add(field,c);
                   
                   c.gridy++;
                   panel.add(table,c);
                   c.gridx++;
                  
                   panel.add(button,c);
                   panelList.add(panel);
                   //buttonList.add(button);
                   j++;
                }	    
	}
	
        public void showTables()
        {
            this.setLayout(new GridBagLayout());
                GridBagConstraints c = new GridBagConstraints();
                c.fill = GridBagConstraints.HORIZONTAL;
                c.gridx=0;
                c.gridy=0;
            for(JPanel p: panelList)
            {
                
                
                this.add(p,c);
                c.gridy++;
            }
            //this.repaint();
        }
        
        public Trip getChosenTrip()
        {
            return chosenTrip;
        }
        
        public JPanel getPanel()
        {
            return null;
        }
        
	public void actionPerformed(ActionEvent e) 
	{
            System.out.println("mmm!!! "+e.getActionCommand());
            String tripid=e.getActionCommand();
            chosenTrip=buttonMap.get(tripid);
            getChosenTrip();
          
//		if(e.getActionCommand()==recipeLoadButton.getActionCommand())
//		{
//			
//			int returnVal = chooser.showOpenDialog(recipeLoadButton);
//			if(returnVal == JFileChooser.APPROVE_OPTION) {
//			       RecipeHandler.addRecipe(chooser.getSelectedFile());
//
//			    }
//		}
            
            
	}
	
	public void windowActivated(WindowEvent e){}
     
	public void 	windowClosed(WindowEvent e){}
     
	public void 	windowClosing(WindowEvent e){
		
		System.exit(0);
	}
     
	public void 	windowDeactivated(WindowEvent e){}
     
	public void 	windowDeiconified(WindowEvent e){}
    
	public void 	windowIconified(WindowEvent e){}
    
	public void 	windowOpened(WindowEvent e){}
    
	
}
